CKEDITOR.plugins.add('variabletype',
{
    requires: ['richcombo'], //, 'styles' ],
    init: function (editor) {
        var config = editor.config,
         lang = editor.lang.format;

        // Gets the list of tags from the settings.
        var tags = []; //new Array();
        tags[0] = ["[OPPORTUNITY_NAME]", "Opportunity Name", "Opportunity Name"];
        tags[1] = ["[COMPANY_NAME]", "Company Name", "Company Name"];
        tags[2] = ["[COMPANY_ADDRESS]", "Company Address", "Company Address"];
        tags[3] = ["[OFFICE_ADDRESS]", "Office Address", "Office Address"];
        tags[4] = ["[ADVISOR_NAME]", "Advisor Name", "Advisor Name"];
        tags[5] = ["[LOGO]", "Logo", "Logo"];
        tags[6] = ["[REPORT_TITLE]", "Report Title", "Report Title"];
        tags[7] = ["[REPORT_DATE]", "Report Date", "Report Date"];
        // Create style objects for all defined styles.
        editor.ui.addRichCombo('variabletype',
         {
             label: "Variables",
             title: "Variables",
             voiceLabel: "Variables",
             className: 'cke_format',
             multiSelect: false,
             panel:
            {
                css: [config.contentsCss, CKEDITOR.getUrl(editor.skinPath + 'editor.css'), this.path + 'cffm-style.css'],
                voiceLabel: lang.panelVoiceLabel,
            },

             init: function () {
                 for (var this_tag in tags) {
                     this.add(tags[this_tag][0], tags[this_tag][1], tags[this_tag][2], tags[this_tag][3], tags[this_tag][4], tags[this_tag][5]);
                 }
             },

             onClick: function (value) {
                 editor.focus();
                 editor.fire('saveSnapshot');
                 
                 var selection = editor.getSelection().getStartElement();
                 if (!selection.is("img")) {
                     if (!(selection.is("div") &&
                         selection.hasClass("layer"))) {
                         editor.insertHtml(value);
                     } else {
                         CKEDITOR.dom.element.get(selection).appendText(value);
                     }
                     return;
                 } else {
                     var width = selection.$.clientWidth + 'px';
                        var height = selection.$.clientHeight + 'px';
                        var top = selection.$.offsetTop + 'px';
                        var left = selection.$.offsetLeft + 'px';
                        var style = "style = 'padding:0px;position:absolute;height:" + height + ";width:" + width + ";top:" + top + ";left:" + left + "'";

                        // Move cursor to end
                        var range = editor.createRange();
                        range.moveToPosition(range.root, CKEDITOR.POSITION_BEFORE_END);
                        editor.getSelection().selectRanges([range]);

                        editor.insertHtml("<div class='layer' " + style + " _moz_resizing='false'>"+value+"</div>");
                 }
                 
                 editor.fire('saveSnapshot');
             }
         });
    }
});
